package com.yixuan.mt.console.controller;

import com.yixuan.mt.common.entity.Policy;
import com.yixuan.mt.common.entity.User;
import com.yixuan.mt.common.response.Result;
import com.yixuan.mt.common.request.PostIdentityRequest;
import com.yixuan.mt.common.service.IdentityService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/op/identity")
public class IdentityController {

    @Autowired
    private IdentityService identityService;

    /**
     * 新增用户
     */
    @PostMapping
    public Result<?> postIdentity(@Valid @RequestBody PostIdentityRequest postIdentityRequest) {
        identityService.postIdentity(postIdentityRequest);
        return Result.success();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteIdentity(@PathVariable Integer id) {
        identityService.deleteIdentity(id);
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/password")
    public Result<?> updatePassword(@Valid @RequestBody User user) {
        identityService.updatePassword(user);
        return Result.success();
    }

    /**
     * 获取用户列表
     */
    @GetMapping
    public Result<List<User>> getIdentityList() {
        return Result.success(identityService.getIdentityList());
    }

    /**
     * 获取用户权限
     */
    @GetMapping("/policies/{id}")
    public Result<List<Policy>> getUserPoliciesList(@PathVariable Integer id) {
        return Result.success(identityService.getUserPoliciesList(id));
    }
}
